//
//  AIInfoFirstTableCell.m
//  MiaMind
//
//  Created by Vill on 2023/12/15.
//

#import "AIInfoFirstTableCell.h"

@implementation AIInfoFirstTableCell

+(instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"AIInfoFirstTableCell";
    AIInfoFirstTableCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
       cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
    }
    return cell;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // 创建一个 NSMutableAttributedString 对象，设置占位符文字的属性
    NSString *placeholderText = @"Your Nickname *";
    NSMutableAttributedString *attributedPlaceholder = [[NSMutableAttributedString alloc] initWithString:placeholderText];
    [attributedPlaceholder addAttribute:NSForegroundColorAttributeName value:[UIColor colorHex:@"#FFFFFF" alpha:0.5] range:NSMakeRange(0, placeholderText.length-2)];
    [attributedPlaceholder addAttribute:NSFontAttributeName value:[UIFont fontWithWeight:MTBold size:PTW(20)] range:NSMakeRange(0, placeholderText.length)];
    // 设置星号的颜色为红色
    NSRange range = [placeholderText rangeOfString:@"*"];
    if (range.location != NSNotFound) {
        [attributedPlaceholder addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(range.location, 1)];
    }
    // 将 NSMutableAttributedString 对象赋值给 UITextField 的 attributedPlaceholder 属性
    self.titLbl.placeholder = nil; // 先将原始的占位符置空
    self.titLbl.attributedPlaceholder = attributedPlaceholder; // 设置新的占位符
    self.titLbl.textColor = UIColor.whiteColor;
    self.titLbl.font = [UIFont fontWithWeight:MTBold size:PTW(20)];
    [self.titLbl setModifyClearButton:@"ai_main_edit_close"];
    self.titLbl.delegate = self;
}

// 代理方法
 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.text.length - range.length + string.length > 30){
        return NO;
    }
    return YES;
}

#pragma mark ====== 结束编辑 ======
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (self.textfieldEndEditBlock){
        self.textfieldEndEditBlock(textField.text);
    }
}

@end
